package com.hdc.lw.word_server.service;

import com.hdc.lw.word_server.mapper.CollectMapper;
import com.hdc.lw.word_server.mapper.WordMapper;
import com.hdc.lw.word_server.po.Word;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WordService {

    @Autowired
    private WordMapper wordMapper;
    @Autowired
    private CollectMapper collectMapper;

    public Map insertWord(Word word){
        Map<String,Object> map = new HashMap();
        int row = wordMapper.insertWord(word);
        if(row>0){
            map.put("code",200);
            map.put("msg","单词添加成功！");
        }else {
            map.put("code",301);
            map.put("msg","单词添加失败！");
        }
        return map;
    }

    public Map getWordList(Integer uid,Integer size,Integer page){
        Map<String,Object> map = new HashMap<>();
        List list = wordMapper.getWordList((page-1)*size,size);
        List wid = collectMapper.getcollects(uid);
        ArrayList arrayList = new ArrayList(wid);
        for(Object word :list){
            Word wd = (Word) word;
            if(wid.indexOf(wd.getId())!=-1){
                wd.setIscollect(true);
            }
        }
        Integer rownum = wordMapper.getRowNum();
        if(list!=null){
            map.put("code",200);
            map.put("msg","查询成功！");
            map.put("wordAttr",list);
            map.put("count",rownum);
        }else {
            map.put("code",301);
            map.put("msg","查询失败！");
        }
        return map;
    }

    public Word getWordByName(String name){
        return wordMapper.getWordByName(name);
    }

    public Map delWordById(Integer id){
        Map<String,Object> map = new HashMap<>();
        int row = wordMapper.delWordById(id);
        if(row>0){
            map.put("code",200);
            map.put("msg","删除成功！");
        }else {
            map.put("code",301);
            map.put("msg","删除失败！");
        }
        return map;
    }

    public Map updateWordByid(Word word){
        Map<String,Object> map = new HashMap<>();
        int row = wordMapper.updateWordByid(word);
        if(row>0){
            map.put("code",200);
            map.put("msg","修改成功！");
        }else {
            map.put("code",301);
            map.put("msg","修改失败！");
        }
        return map;
    }
}
